package com.vo;

import java.util.List;

public class Page {
	int pageSize;
	int pageNo;
	int rowCount;
	int pageCount;
	List dataList;
	String pageNav;
	
	public Page(){
	}
	public Page(int pageSize,int pageNo,int rowCount,List dataList){
		this.pageSize=pageSize;
		this.pageNo=pageNo;
		this.rowCount=rowCount;
		this.dataList=dataList;
		this.getPageNav();
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public int getRowCount() {
		return rowCount;
	}
	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}
	public List getDataList() {
		return dataList;
	}
	public void setDataList(List dataList) {
		this.dataList = dataList;
	}
	
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	public String getPageNav() {
		String strTemp1 = "";
		String strTemp2 = "";
		String strBlock1 = "";
		String strBlock2 = "";
		int i;
		StringBuffer bffPageNav=new StringBuffer();
		int pageCount;
		boolean haveLastPage;
		boolean haveFirstPage; 
		int nextPage;				//下页数
		int prePage;				//前页数
		int lastPage;				//最后一页
		int firstPage;			//第一页
		if(pageNo<1) pageNo=1;
		
		if(rowCount == 0)
		{
			firstPage = -1;
			haveFirstPage = false;
			lastPage = -1;
			haveLastPage = false;
			pageCount = 0;
			prePage = -1;
			nextPage = -1;
		}
		else
		{ 
			if(rowCount%pageSize == 0)
			{
				pageCount = rowCount/pageSize;
			}
			else
			{
				pageCount = rowCount/pageSize + 1;
			}

			firstPage = 1;
			haveFirstPage = true;
			lastPage = pageCount;
			
			if(pageNo == 1)
			{
				prePage = -1;
				haveFirstPage = false;
				haveLastPage = false;
			}
			else
			{
				prePage = pageNo -1;
				haveLastPage = true;
			}
			if(pageNo == pageCount)
			{
				nextPage = -1;
				haveLastPage = false;
			}
			else
			{
				nextPage = pageNo + 1;
				haveLastPage = true;
			}
		}

		if(rowCount != 0)
		{
			bffPageNav.append("<select name='page' size='1' onChange='movePage(this.value)'>");
			for(i=1;i<=pageCount;)
			{
				if(pageNo==i)
				{
					bffPageNav.append("<option value='"+Integer.toString(pageNo)+"' selected>"+Integer.toString(pageNo)+"</option>");
				}
				else
				{
				    bffPageNav.append("<option value='"+Integer.toString(i)+"'>"+Integer.toString(i)+"</option>");
				}
				if(pageNo<=i+100&&pageNo>i-100)
					i++;
				else if(pageNo>=i-1000 && pageNo<= i+1000)
					i=(i/100)*100+100;
				else
					i=(i/1000)*1000+1000;
			}
			//pageNav=pageNav+"</select>";
			bffPageNav.append("</select>");
			if(!haveFirstPage)
			{
				prePage=pageNo;
			}
				strBlock1 = "&nbsp;<a href='#' onClick='movePage("+Integer.toString(firstPage)+")'>第一页</a>&nbsp;<a href='#' onClick='movePage("+ Integer.toString(prePage) +")'>上一页</a>&nbsp;";

			if(!haveLastPage)
			{
				nextPage=pageNo;
			}
				strBlock2 = "<a href='#' onClick='movePage("+ Integer.toString(nextPage) +")'>下一页</a>&nbsp;<a href='#' onClick='movePage("+Integer.toString(lastPage)+")'>最后页</a>&nbsp;";

			if(pageNo*pageSize < rowCount)
			{
				strTemp2 = Integer.toString(pageNo*pageSize);
			}
			else
			{
				strTemp2 = Integer.toString(rowCount);
			}

			strTemp1 = Integer.toString((pageNo-1) * pageSize+1);	
			strTemp1 = "<td align=left valign=bottom width=54% style='border:0px;white-space: nowrap;'>第 "+ strTemp1 + " / " + strTemp2 +" 条（共 " + Integer.toString(rowCount) + " 条） 第 "+ Integer.toString(pageNo) + " / " + Integer.toString(pageCount) + " 页</td>";
			pageNav ="<td align=right valign=bottom width=46% style='border:0px;white-space: nowrap;'>"+strBlock1 + strBlock2+"第 "+bffPageNav.toString() +" 页</td>";
			pageNav = strTemp1 + pageNav;
		}else
			pageNav="<td align=left valign=bottom width=54% style='border:0px;white-space: nowrap;'>第 0 / 0 条（共 0 条） 第 0 / 0 页</td><td align=right valign=bottom width=46% stype='border:0px;'>&nbsp;<a href='#' >第一页</a>&nbsp;<a href='#' >上一页</a>&nbsp;<a href='#'>下一页</a>&nbsp;<a href='#' >最后页</a>&nbsp;第  <select name='page' size='1'></select> 页</td>";
		pageNav="<table width=\"100%\" border=\"0\"><tr style='border:0px;'>"+pageNav+"</tr></table>";
		return pageNav;
	}
	public void setPageNav(String pageNav) {
		this.pageNav = pageNav;
	}
}
